package com.hotel.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hotel.entidade.Funcionario;

public class FuncionarioDAO {

	private Session session;

	public FuncionarioDAO(Session session) {
		this.session = session;
	}

	public void insere(Funcionario funcionario) {
		session.save(funcionario);
		session.beginTransaction().commit();
		session.close();
	}

	public void deleta(Funcionario funcionario) {
		session.delete(funcionario);
		session.beginTransaction().commit();
	}

	public void atualisa(Funcionario funcionario) {
		session.update(funcionario);
		session.beginTransaction().commit();
	}

	public Funcionario buscaPorId(int id) {
		return (Funcionario) session.load(Funcionario.class, id);
	}

	public List<Funcionario> pesquisarFuncionario(String keyword) {
		@SuppressWarnings("unchecked")
		List<Funcionario> list = session.createCriteria(Funcionario.class)
				.add(Restrictions.or(
						Restrictions.ilike("cpf", keyword, MatchMode.ANYWHERE),
						Restrictions.ilike("nome", keyword, MatchMode.ANYWHERE)))
				.addOrder(Order.asc("nome")).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Funcionario> listaTodos() {
		return session.createQuery("FROM Funcionario ORDER BY nome ASC").list();
		// return session.createCriteria(Usuario.class).list();
	}
	
	public List<Funcionario> login(String login, String senha) {
		@SuppressWarnings("unchecked")
		List<Funcionario> list = session.createCriteria(Funcionario.class)
			.add(Restrictions.and(
				Restrictions.ilike("login", login, MatchMode.EXACT),
				Restrictions.ilike("senha", senha, MatchMode.EXACT))).list();
		return list;
	}
}
